class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int n=arr.size();
        int cur=0;int dest=-1;
        while(cur<n)
        {
            if(arr[cur]==0)dest+=2;
            else
            dest++;
            if(dest>=n-1)break;
            cur++;
        }
        if(dest==n)
        {
            dest--;
            arr[dest--]=0;
            cur--;
        }
        while(dest>=0)
        {
            if(arr[cur]==0)
            {
                arr[dest--]=arr[cur];
                arr[dest--]=arr[cur--];
            }
            else
            arr[dest--]=arr[cur--];
        }
    }
};
